package com.mzkj.wanjulaile.mobile.api.modules.account.service;

import com.google.common.collect.Lists;
import com.mzkj.common.utils.Collections3;
import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.modules.account.constant.Constant;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.BuyVip;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.VipScheme;
import com.mzkj.wanjulaile.mobile.api.modules.account.mapper.BuyVipMapper;
import com.mzkj.wanjulaile.mobile.api.modules.account.mapper.VipSchemeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * accountService
 * Created by wangxw on 2016/12/12.
 */
@Service("VipSchemeService")
public class VipSchemeServiceImpl implements VipSchemeService {
    @Autowired
    private VipSchemeMapper vipSchemeMapper;
    @Autowired
    private BuyVipMapper buyVipMapper;

    public VipScheme get(String id) {
        return vipSchemeMapper.get(id);
    }

    public VipScheme getVipDetail(String id, String accountId) {
        VipScheme vipScheme = vipSchemeMapper.get(id);
        Integer deposit = buyVipMapper.calDepositByAccountId(accountId);
        if (deposit != null && deposit != 0) {
            vipScheme.setDeposit(0);
        }
        return vipScheme;
    }

    public List<VipScheme> findList(VipScheme vipScheme) {
        return vipSchemeMapper.findList(vipScheme);
    }

    public List<VipScheme> findVipSchemes(VipScheme vipScheme, String id) {
        List<VipScheme> vipSchemes = vipSchemeMapper.findList(vipScheme);
        Integer totalDeposit = buyVipMapper.calDepositByAccountId(id);
        // 只有新用户能购买体验会员 只要成功支付的有购买记录就不显示该套餐了
        List<BuyVip> allList = buyVipMapper.findListByAccountId(id);
        List<VipScheme> delVipSchemes = Lists.newArrayList();
        if (!Collections3.isEmpty(allList)) {
            for(int i = 0; i < vipSchemes.size(); i ++) {
                VipScheme vipScheme2 = vipSchemes.get(i);
                if (Constant.TEMP_VIP.equals(vipScheme2.getType())) {
                    delVipSchemes.add(vipScheme2);
                    continue;
                }
                if(totalDeposit != null && totalDeposit != 0) {
                    vipScheme2.setDeposit(0);
                }
                //不显示月度会员
                if (Constant.MONTH_VIP.equals(vipSchemes.get(i).getType())){
                    delVipSchemes.add(vipScheme2);
                    continue;
                }
            }
        }
        for (VipScheme scheme : vipSchemes) {
            //不显示月度会员
            if (Constant.MONTH_VIP.equals(scheme.getType())) {
                delVipSchemes.add(scheme);
            }
        }
        vipSchemes.removeAll(delVipSchemes);
        return vipSchemes;
    }


    public VipScheme getByType(Integer type) {
        return vipSchemeMapper.getByType(type);
    }
}
